import 'package:get/get.dart';
import 'package:kkhousekeep/models/authorization_community_entity.dart';

import 'information_page.dart';

class AuthorizationHomeController extends GetxController {
  final String city;
  final Community community;

  AuthorizationHomeController(
    this.city,
    this.community,
  );

  ///当前列表的状态
  var listDataType = ListType.building;

  ///显示的列表数据
  var listData = [].obs;

  ///选择的楼栋
  var communityBuilding = "".obs;

  ///选择的单元
  var unit = "".obs;

  ///选择房间
  var room = "".obs;

  ///选择社区的楼栋数据
  late Building selectedBuilding;

  ///选择楼栋的单元数据
  late Unit selectUnit;

  ///选择的单元的房间数据
  late Room selectRoom;

  @override
  void onInit() {
    super.onInit();
    if (community.buildings != null) {
      var buildingList = community.buildings;
      for (var building in buildingList!) {
        listData.add(building.buildingName);
      }
    }
  }

  ///在不同进度下的处理
  void select(String content, int index) {
    //选择楼栋状态
    if (listDataType == ListType.building) {
      listDataType = ListType.unit;
      communityBuilding.value = content;
      selectedBuilding = community.buildings![index];
      listData.clear();
      if (selectedBuilding.units != null) {
        var unitList = selectedBuilding.units;
        for (var unitInformation in unitList!) {
          listData.add(unitInformation.unitName);
        }
      }
      return;
    }
    //选择单元状态
    if (listDataType == ListType.unit) {
      listDataType = ListType.room;
      unit.value = content;
      selectUnit = selectedBuilding.units![index];
      listData.clear();
      if (selectUnit.rooms != null) {
        var roomList = selectUnit.rooms;
        for (var roomInformation in roomList!) {
          listData.add(roomInformation.roomName);
        }
      }
      return;
    }
    //选择房间状态
    if (listDataType == ListType.room) {
      selectRoom = selectUnit.rooms![index];
      Get.to(
        () => BuildInformationPage(
          communityId: community.communityId,
          roomId: selectRoom.roomId,
        ),
      );
    }
  }

  ///修改状态为选择楼栋
  void changeListTypeAsBuilding() {
    listDataType = ListType.building;
    listData.clear();
    var buildingList = community.buildings!;
    for (var buildingInformation in buildingList) {
      listData.add(buildingInformation.buildingName);
    }
    communityBuilding.value = "";
    unit.value = "";
  }

  ///修改状态为选择单元
  void changeListTypeAsUnit() {
    listDataType = ListType.unit;
    listData.clear();
    var unitList = selectedBuilding.units;
    for (var unitInformation in unitList!) {
      listData.add(unitInformation.unitName);
    }
    unit.value = "";
  }
}

///列表数据的状态
enum ListType { building, unit, room }
